package com.richfit.barcodesystem_cczt.fragments.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.richfit.barcodesystem_cczt.R;
import com.richfit.barcodesystem_cczt.databinding.LayoutFragmentDetailBinding;
import com.richfit.barcodesystem_cczt.fragments.model.BatchWareHousingEntity;

import java.util.List;

/**
 * @author cl 20211117
 */
public class BWH_Detail_Adapter extends BaseAdapter {
    private static final String TAG = "Detail_Adapter";
    private Context context;
    private List<BatchWareHousingEntity> list;
    /**
     * 记录最后的位置
     */
    private int lastX = 0;
    private int initX = 0;

    public BWH_Detail_Adapter(Context context, List<BatchWareHousingEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public BatchWareHousingEntity getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutFragmentDetailBinding binding;
        if (convertView == null) {
            binding = LayoutFragmentDetailBinding.inflate(LayoutInflater.from(context));
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (LayoutFragmentDetailBinding) convertView.getTag();
            convertView = binding.getRoot();
        }
        binding.TxtBoxNo.setText(getItem(position).getRefNum());
        if (getItem(position).getIsChecked()) {
            binding.CbChoose.setImageResource(R.mipmap.cb_yes);
            binding.LLItem.setBackgroundResource(R.drawable.shape_item_radius_green);
        } else {
            binding.CbChoose.setImageResource(R.mipmap.cb_no);
            binding.LLItem.setBackgroundResource(R.drawable.shape_item_radius);
        }
        //是否能执行点击事件
        final Boolean[] isClick = {true};
        final Boolean[] isStop = {false};
        binding.LLItem.setOnTouchListener((v, event) -> {
            Log.i(TAG, "onBindViewHolder: " + isStop[0]);
//            if (isStop[0]) {
//                mitemRemoveListener.onItemRemove(position, getItem(position));
//                return false;
//            }
            switch (event.getAction()) {
                case (MotionEvent.ACTION_DOWN):
                    //按下按钮，记录开始移动标记
                    lastX = (int) event.getRawX();
                    initX = (int) event.getRawX();
                    Log.i(TAG, "onBindViewHolder: ACTION_DOWN");
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.i(TAG, "onBindViewHolder: ACTION_MOVE");

                    isClick[0] = false;
                    int x = (int) (event.getRawX() - lastX);
                    if (x < 0) {
                        int y = 0;// (int) (event.getRawY() - lastY);
                        int l = v.getLeft() + x;
                        int r = v.getRight() + x;
                        int t = v.getTop() + y;
                        int b = v.getBottom() + y;
                        v.layout(l, t, r, b);
                        lastX = (int) event.getRawX();
                        Log.i(TAG, "onBindViewHolder: ACTION_MOVE11111：" + Math.abs(initX - lastX));
                        Log.i(TAG, "onBindViewHolder: ACTION_MOVE22222：" + v.getWidth() / 2);

                        if (Math.abs(initX - lastX) > v.getWidth() / 2) {
                            Log.i(TAG, "onBindViewHolder: " + "删除");
                            isStop[0] = true;
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (isClick[0]) {
//                        if (mItemClickListener != null) {
//                            // 这里利用回调来给RecyclerView设置点击事件
//                            mItemClickListener.onItemClick(position, getItem(position));
//                        }
                    } else {
                        isClick[0] = true;
                        this.notifyDataSetChanged();
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                    isClick[0] = true;
                    this.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
            return false;
        });
        return convertView;
    }


}
